/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import DTO.Object.MngOrder.MngOrders;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author TuPA
 */
public class OrderModel {

    public static boolean updateStatusOrder(MngOrders mngOrder, String id) {
        DBUtil util = new DBUtil();
        Connection conn = null;
        Statement stm = null;
        String query = null;
        String queryPro = null;
        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            stm = conn.createStatement();
            for (int i = 0; i < mngOrder.getMngOrder().size(); i++) {
                if (mngOrder.getMngOrder().get(i).getOrderID() == Integer.valueOf(id)) {
                    query = "Update OrderPayment SET"
                            + " orderStatus = " + mngOrder.getMngOrder().get(i).getOrderStatus()
                            + " where orderID = '" + id + "'";
                    if (mngOrder.getMngOrder().get(i).getOrderStatus() == 2) {
                        queryPro = "Update Product SET"
                                + " Product.proNumber=Product.proNumber+ " + mngOrder.getMngOrder().get(i).getTotalQuantity()
                                + " where Product.proID in ("
                                + " select OrderDetail.proID from OrderDetail"
                                + " where OrderDetail.orderMother= '" + id + "')";
                        stm.executeUpdate(queryPro);
                    }
                    stm.executeUpdate(query);
                    stm.close();
                    conn.close();
                    return true;
                }
            }
        } catch (Exception e) {
        }
        return false;
    }

    public static void searchAndModify(Node node, int id, String orderStatus) {
        if (node == null) {
            return;
        }
        if (node.getNodeName().equals("MngOrder")) {
            NodeList children = node.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node tmp = children.item(i);
                String orderMother = "";
                if (tmp.getNodeName().equals("orderID")) {
                    orderMother = tmp.getTextContent().trim();
                    if (Integer.valueOf(orderMother) != id) {
                        break;
                    }
                } else if (tmp.getNodeName().equals("orderStatus")) {
                    tmp.setTextContent(orderStatus);
                    return;
                }
            }
        }
        NodeList children = node.getChildNodes();
        int i = 0;
        while (i < children.getLength()) {
            searchAndModify(children.item(i++), id, orderStatus);
        }
    }

    

}
